<?php

namespace app\mobile\controller;

use app\BaseController;
use app\mobile\model\Opinion;
use app\Request;
use think\db\Query;
use think\facade\Validate;

class OpinionController extends BaseController
{

    /**意见反馈列表
     * @fun:
     * @author:崔彦雷
     * @time:2022/12/7 下午 2:42
     */
    public function opinionList(Request $request){

        $param = $request->param();
        $validate = Validate::rule([
            'perPage|每页数量' => 'require|number',
        ]);
        if (!$validate->check($param)){
            return $this->err_json($validate->getError());
        }

        $admin_id = $this->userData['data']->id;
        $param['admin_id'] = $admin_id;

        $info = Opinion::where('1=1')
            ->where(function (Query $query) use ($param){
                $query->where('admin_id',$param['admin_id']);
            })
            ->order('id desc')
            ->paginate($param['perPage']);


        return $this->suc_json('',$info);

    }

    /**添加意见反馈
     * @fun:
     *
     * @param Request $request
     * @author:崔彦雷
     * @time:2022/12/7 下午 1:46
     */
    public function addOpinion(Request $request){
        $param = $request->param();
        $validate = Validate::rule([
            'content|反馈内容' => 'require|max:450',
            'type|类型' => 'require|number',
        ]);
        if (!$validate->check($param)){
            return $this->err_json($validate->getError());
        }

        $admin_id = $this->userData['data']->id;

        $opinion = new Opinion();
        $opinion->admin_id = $admin_id;
        $opinion->content = $param['content'];
        $opinion->type = $param['type'];
        $info = $opinion->save();

        if ($info == 1) {
            return $this->suc_json('保存成功');
        }else{
            return $this->err_json('保存失败');
        }
    }

}